import { getToken, setToken, removeToken } from '@/views/utils/auth'
import { loginAPI, getUserInfo } from '@/api/user'
const state = {
  token: getToken() || '',
  profile: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setProfile(state, profile) {
    state.profile = profile
  }
}
const actions = {
  async loginAction(context, data) {
    const res = await loginAPI(data)
    context.commit('setToken', res.data.token)
  },
  logout(context) {
    context.commit('removeToken')
  },
  async getProfileAction(context) {
    const res = await getUserInfo()
    context.commit('setProfile', res.data)
    return res.data
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
